home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16419 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  65 lines

  1. Path: qualcomm.com!not-for-mail
  2. From: drew@qualcomm.com (Drew Eckhardt)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: sprintf() in C++
  5. Date: 10 Apr 1996 14:18:19 -0600
  6. Organization: QUALCOMM, Incorporated; Boulder, CO, USA
  7. Message-ID: <4kh52b$34t@qualcomm.com>
  8. References: <4kene4$nkb@post.gsfc.nasa.gov> <4kf5bg$u8a@mule2.mindspring.com>
  9. NNTP-Posting-Host: littlebear.qualcomm.com
  10.  
  11. In article <4kf5bg$u8a@mule2.mindspring.com>,
  12. Justin Rudd <rudd@mindspring.com> wrote:
  13. >tycho <vonrosen@nssdca.gsfc.nasa.gov> wrote:
  14.  
  15. I use dynamically allocated strstream buffers where practical, since
  16. space isn't wasted, there's no limit on the output size, and no "magic"
  17. size number which some one may have to increase when more output is added
  18. in the future.
  19.  
  20. Instead of this,
  21.  
  22. >char msg[500];
  23. >ostrstream stream(msg,500);
  24.  
  25. I'd say
  26.  
  27.    ostrstream stream;
  28.  
  29. >stream << "An Int Value" << 10 << endl;
  30. >stream << "A Float Value" << 10.0f << endl;
  31. >stream << "Really Simple stuff" << ends << endl;
  32.  
  33. And this
  34.  
  35. >cout << msg;
  36.  
  37. would become
  38.  
  39.    cout << stream.str();
  40.  
  41. followed by 
  42.  
  43.    stream.rdbuf()->freeze(0);
  44.  
  45. to deallocate the buffer after use.  Alternatively, you can delete the 
  46. buffer when you're done using it, doing something like this:
  47.  
  48.    char *
  49.    foo () {
  50.       ostrstream bar;
  51.       bar << "Replace with arbitrary meaningful stuff." << ends;
  52.       return (bar.str());
  53.    }
  54.  
  55. ...
  56.  
  57.    char *tmp = foo();
  58.    // operate on tmp here.
  59.    delete tmp;
  60.  
  61. -- 
  62. <a href="http://www.poohsticks.org/drew/">Home Page</a>
  63. Four boxes : soap, ballot, jury, ammo.  | Work: drew@Qualcomm.COM       
  64. Use in that order.                      | Play: drew@PoohSticks.ORG    
  65.